#include <string>
#include <mutex>
#include "jsoncpp/json.h"
#include <functional>
#include "Network/Socket.h"

using namespace std;
using namespace Fdt;

class FileTransfer{
public:
	static FileTransfer *instance(){
		if(sm_file_transfer == nullptr){
			std::lock_guard<std::mutex> guard(sm_fileMutex);
			sm_file_transfer = new FileTransfer();
		}
		return sm_file_transfer;
	}

	bool fileVerify(string filename, const char *md5);

	bool streamWrite(string filename, const void *buffer, size_t size);

	bool fileStreamReset(string filename, string Type);

	bool GetFileInfo(Json::Value table ,string &filename,  unsigned int &size, string &md5);

	bool CloudUploadStream(Json::Value table, function<bool(string file, string url, function<void(const SockException &ex)> cb)> callback);

	int getCloudUpMark();
	
	bool setCloudUpMark(int mark);
	
	~FileTransfer();
private:
	FileTransfer();

	bool getFileDir(string filename, string Type, string &fileDir);

	bool getFileName(string path, string postfix, string &filename);


	bool exeCmd(string cmd);
	
private:
	string                      m_filename;
	//判断是否存在同名的文件
	bool                        m_flag_retransfer = false;
	int  						m_fd = -1;
	//文件所用的业务类型
	string 						m_type;
	bool 						m_unzip;

	int 						m_cloudUpMark = 0;
	
	static const string			sm_filedir;
	static FileTransfer         *sm_file_transfer;
	static mutex				sm_fileMutex;

	const string m_streamer = "Streamer";
	const string m_curtain  = "Curtain";
	
	string m_md5_val;
};
